package crm.implementor;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Statement;

import crm.daoInterfaces.CampaignDao;
import crm.dbhandelling.Connections;
import crm.dbhandelling.Crude;
import crm.model.Campaign;


public class CampaignImp extends Crude implements CampaignDao
{

	private String className= new Campaign().getClass().getName();
	@Override
	public void add(Campaign camp) throws Exception{
	
		super.insert(camp);
		
	}

	@Override
	public void update(Campaign camp) throws Exception
	{
		
		super.update(className, "campaignName", camp.getCampaignName(), "autocampaignId", camp.getAutocampaignId()+"");
		super.update(className, "campaignStartDate", camp.getCampaignStartDate()+"", "autocampaignId", camp.getAutocampaignId()+"");
		super.update(className, "campaignEndDate", camp.getCampaignEndDate()+"", "autocampaignId", camp.getAutocampaignId()+"");
		super.update(className, "createdBy", camp.getCreatedBy(), "autocampaignId", camp.getAutocampaignId()+"");
		super.update(className, "description", camp.getDescription(), "autocampaignId", camp.getAutocampaignId()+"");
		super.update(className, "status", camp.getStatus()+"", "autocampaignId", camp.getAutocampaignId()+"");
		
	}

	@Override
	public void delete(Campaign camp) {
	
		super.delete(className, "autocampaignId", camp.getAutocampaignId()+"");
		
	}

	

	@Override
	public Campaign[] retrieveAll() throws Exception {
		

		Object[] att=super.retreiveAll(className);
		Campaign[] s=new Campaign[att.length];
		for(int i=0;i<s.length;i++)
				s[i]=(Campaign) att[i];
		return s;
		
	}

	@SuppressWarnings("unused")
	@Override
	public Campaign[] retriveOnMultipleCondition(Date start, Date end,
			String createdBy, String status) throws Exception {
		    
		    String baseSql = "SELECT * FROM CAMPAIGN WHERE ";
		    String dateFilter = " WHERE CAMPAIGNSTARTDATE = "+"'"+start+"'"+" AND CAMPAIGNENDDATE = "+"'"+end+"'";
		    String createdByFilter = " CREATEDBY = "+"'"+createdBy+"'";
		    String statusFilter = " STATUS = 1";
		    
		    
		    String sql = baseSql;
		    createdBy=createdBy.toUpperCase();
		    if(createdBy.equals("ALL")==false)
		    	sql=sql+createdByFilter+"AND "+statusFilter;
		    else
		    	sql=sql+"AND "+statusFilter;
		    
		     Statement st = Connections.getMyStatement();
		     ResultSet rs = st.executeQuery(sql);
		     int count =0;
		     while(rs.next())
		     {
                    count++;
		     }
		    Campaign[]campaigns = new Campaign[count];
		    rs = st.executeQuery(sql);
		    int i =0;
		    while(rs.next())
		    {
		    	campaigns[i].setAutocampaignId(rs.getInt("autoCampaignId"));
		    	campaigns[i].setCampaignEndDate(rs.getDate("campaignEndDate"));
		    	campaigns[i].setCampaignStartDate(rs.getDate("campaignStartDate"));
		    	campaigns[i].setCampaignName(rs.getString("campaignName"));
		    	campaigns[i].setCreatedBy(rs.getString("createdBy"));
		    	campaigns[i].setDescription(rs.getString("description"));
		    	campaigns[i].setStatus(rs.getInt("status"));
		    	i++;
		    }
		    
		    
		    return campaigns;
		
		
	}

	@Override
	public Campaign retrieve( String conditionField,
			String conditionValue) throws Exception {
		
		Campaign c=(Campaign) super.retreive(className, conditionField, conditionValue);
		return c;
		
	}

	@Override
	public Campaign[] retrieveArray(
			String conditionField, String conditionValue) throws Exception {
		
		Object[] att=super.retreiveArrayOnCondition(className, conditionField, conditionValue);
		Campaign[] s=new Campaign[att.length];
		for(int i=0;i<s.length;i++)
				s[i]=(Campaign) att[i];
		return s;
		
	}
	@Override
  public int getAutoGenratedKey()
  {
	 int key=super.getAutoGenratedKey(className,"autocampaignId") ;
	 return key;
  }
	

}
